package com.wwy.arithmetic.suanfa;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 同构字符串
 */
public class TongGouString {

    public static void main(String[] args) {
        String a = "abaa";
        Character s = 'a';
        System.out.println("a.indexOf('a') = " + a.indexOf(s));
    }

    static boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        Set<Character> set = new HashSet<>();
        Set<Character> set1 = new HashSet<>();
        Map<Character, Integer> map = new HashMap<>();
        Map<Character, Integer> map1 = new HashMap<>();
        char[] chars = s.toCharArray();
        char[] chars1 = t.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            if (set.add(chars[i])) {
                map.put(chars[i], i);
            }
            if (set1.add(chars1[i])) {
                map1.put(chars1[i], i);
            }
            if (!set.add(chars[i])) {
                Integer integer = map.get(chars[i]);
                if (chars1[integer] != chars1[i]) {
                    return false;
                }
            }
            if (!set1.add(chars1[i])) {
                Integer integer = map1.get(chars1[i]);
                if (chars[integer] != chars[i]) {
                    return false;
                }
            }
        }
        return true;
    }

    public boolean isIsomorphic1(String s, String t) {
        for (int i = 0; i < s.length(); i++) {
            char s1 = s.charAt(i);
            char t1 = t.charAt(i);
            if (s.indexOf(s1) != t.indexOf(t1)) {
                return false;
            }
        }
        return true;
    }
}